
import 'package:exsun_plate_app/mvp/model/get_code_entity.dart';
import 'package:exsun_plate_app/mvp/model/login_model.dart';

import 'login_repository.dart';
import 'package:dio/dio.dart';
import 'package:exsun_plate_app/common/api.dart';

class LoginRepositoryIml implements LoginRepository{
  @override
  Future<LoginModel> fetch(String userName, String password) {
    return _login(userName,password);
  }

  Future<LoginModel> _login(String userName, String password ) async {

    var loginURL = APIS.DEVELOP_URL+APIS.LOGIN;
    Response response;
    //表单提交
    FormData formData = new FormData.from({
      "account": userName,
      "password": password,
    });
    Dio dio = new Dio();
    try {
      response = await dio.post(loginURL,data: formData);
      var data = response.data;
      print(data);
      return LoginModel.fromJson(data);
    } catch (e, s) {
      print(s);
      return null;
    }

    
  }

  @override
  Future<GetCodeEntity> getCode(String phone) {

    return _getCode(phone);
  }

  Future<GetCodeEntity> _getCode(String phone) async {
    var loginURL = APIS.DEVELOP_URL+APIS.GET_CODE;
    Response response;
    //表单提交
    FormData formData = new FormData.from({
      "phone": phone,
    });
    Dio dio = new Dio();
    try {
      response = await dio.post(loginURL,data: formData);
      var data = response.data;
      print(data);
      return GetCodeEntity.fromJson(data);
    } catch (e, s) {
      print(s);
      return null;
    }
  }
  
}